package schoolWork.work05.Example5a;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//用内部类的方式实现
public class PoliceWindow extends JFrame {
    JTextField titleText;
    JPasswordField passwordText;

    PoliceWindow(){
        titleText = new JTextField(10);
        passwordText = new JPasswordField(10);
        passwordText.setEchoChar('*');

        MyActionListener myActionListener = new MyActionListener();
        titleText.addActionListener(myActionListener);
        passwordText.addActionListener(myActionListener);

        setLayout(new FlowLayout());
        add(titleText);
        add(passwordText);

        setBounds(100,100,150,150);
        setVisible(true);
        validate();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    class MyActionListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {
            JTextField textSource = (JTextField) e.getSource();
            if(textSource==titleText) {
                setTitle(titleText.getText());
            }

            else if(textSource==passwordText){
                char c[] = passwordText.getPassword();
                titleText.setText(new String(c));
            }
        }
    }
}
